/**
 * Created by thinkpad on 2017-05-31.
 */

XBox.define("grid.page", {
    title: "webapi列表示例",
    templates: {
        main: ["<div>" +
        "            <div class='grid'></div>" +
        "</div>"],
    },
    elements: {
        grid: ".grid"
    },
    afterRender: function () {
        $.ajaxSetup({
            beforeSend: function (xhr) {
                //debugger;
                xhr.setRequestHeader('DMSP-Net-OData-WebApi-LoginName', 'BasicAuth-Test');
            }
        });
        var that = this;
        window.error_handler = function (e) {
            var error = JSON.parse(e.xhr.responseText);
            XBox.notify.error(error.Message);
        };
        var crudServiceBaseUrl = "http://192.168.10.14:9990//webapi/v1";
        var dataSource = new kendo.data.DataSource({
            type: "webapi",//parameterMap配置对webapi的类型无效，
            transport: {
                read: { url: crudServiceBaseUrl + "/Test/Get" },
                update: { url: crudServiceBaseUrl + "/Test/Update2", type: "post" },
                destroy: { url: crudServiceBaseUrl + "/Test/Delete2", type: "post" },
                create: { url: crudServiceBaseUrl + "/Test/Create2", type: "post" }
            },
            serverPaging: true,
            serverSorting: true,
            serverFiltering: true,
            serverGrouping: true,
            serverAggregates: true,
            pageSize: 10,
            batch: false,//ture表示批量，后台接口的参数必须是类似ApiRequest<T>的模型，false表示单条，后台接口的参数是普通一个目标模型就可以了
            schema: {
                total: "Total",
                data: "Data",
                //error: "apiError",
                model: {
                    id: "ProductID",
                    fields: {
                        ProductID: { editable: false, nullable: true },
                        ProductName: { validation: { required: true } },
                        UnitPrice: { type: "number", validation: { required: true, min: 1 } },
                        Discontinued: { type: "boolean" },
                        UnitsInStock: { type: "number", validation: { min: 0, required: true } }
                    }
                }
            },
            error: error_handler,
            //apiError: function () {
            //    console.log(arguments);
            //    debugger;
            //}
        });

        this.controls.grid.kendoGrid({
            dataSource: dataSource,
            pageable: {
                pageSize: 10,
                refresh: true
            },
            sortable: true,
            groupable: true,
            filterable: {
                operators: {
                    string: {
                        contains: "模糊匹配"
                    },
                    number: {
                        gte: "大于等于",
                        gt: "大于",
                        lte: "小于等于",
                        lt: "小于",
                    }
                }
            },
            height: 500,
            excel: {
                allPages: true,
                fileName: "Products.xlsx"
            },
            toolbar: [
                { name: "create", text: "增",buttonType: "Text" },
                { name: "excel", buttonType: "Text" },
                { name: "pdf", buttonType: "Text" }
            ],
            columns: [
                {
                    field: "ProductName",
                    title: "名称",
                    filterable: {
                        extra: false,
                        // multi:true,
                        // checkAll: false,
                        // dataSource: new kendo.data.DataSource({
                        //     data: [
                        //         {ProductName: "Jane"},
                        //         {ProductName: "Jake"},
                        //         {ProductName: "John"}
                        //     ]
                        // }),
                        ui: function (element) {
                            element.kendoAutoComplete({
                                delay: 0,
                                filter: "contains",
                                dataSource: dataSource,
                                dataTextField: "ProductName"
                            });
                        }
                    }
                },
                { field: "UnitPrice", title: "单价", format: "{0:c}" },
                { field: "UnitsInStock", title: "库存数量" },
                { field: "Discontinued", title: "折扣" },
                {
                    command: [{ name: "edit", text: "改", buttonType: "Text" }, { name: "destroy", text: "删", buttonType: "Text" }],
                    title: "操作",
                }],
            editable: {
                "confirmation": "删除后不能恢复，确定要删除吗?",
                "confirmDelete": "确定",
                "cancelDelete": "取消",
                "mode": "inline",
                "create": true,
                "update": true,
                "destroy": true
            },
        });
    }
});

/*
kendo.syncReady(function() {
    jQuery("#grid")
        .kendoGrid({
            "columns": [
            {
                "title": "Product name",
                "headerAttributes": { "data-field": "ProductName", "data-title": "Product\u0026#32;name" },
                "field": "ProductName",
                "encoded": true,
                "editor": "\u003cinput class=\"k-textbox\" data-val=\"true\" data-val-remote=\"\u0026#39;Product name\u0026#39; is invalid.\" data-val-remote-additionalfields=\"*.ProductName\" data-val-remote-url=\"/aspnet-mvc/Validation/IsProductName_Available\" data-val-required=\"The Product name field is required.\" id=\"ProductName\" name=\"ProductName\" /\u003e\u003cspan class=\"field-validation-valid\" data-valmsg-for=\"ProductName\" data-valmsg-replace=\"true\"\u003e\u003c/span\u003e"
            }, {
                "title": "Unit price",
                "headerAttributes": { "data-field": "UnitPrice", "data-title": "Unit\u0026#32;price" },
                "width": "120px",
                "field": "UnitPrice",
                "format": "{0:C}",
                "encoded": true,
                "editor": "\u003cinput data-val=\"true\" data-val-number=\"The field Unit price must be a number.\" data-val-range=\"The field Unit price must be between 0 and 2147483647.\" data-val-range-max=\"2147483647\" data-val-range-min=\"0\" data-val-required=\"The Unit price field is required.\" id=\"UnitPrice\" max=\"2147483647\" min=\"0\" name=\"UnitPrice\" style=\"width:100%\" type=\"text\" value=\"0\" /\u003e\u003cscript\u003e\r\n\tkendo.syncReady(function(){jQuery(\"#UnitPrice\").kendoNumericTextBox({\"format\":\"c\"});});\r\n\u003c/script\u003e\u003cspan class=\"field-validation-valid\" data-valmsg-for=\"UnitPrice\" data-valmsg-replace=\"true\"\u003e\u003c/span\u003e"
            }, {
                "title": "Units in stock",
                "headerAttributes": { "data-field": "UnitsInStock", "data-title": "Units\u0026#32;in\u0026#32;stock" },
                "width": "120px",
                "field": "UnitsInStock",
                "encoded": true,
                "editor": "\u003cinput data-val=\"true\" data-val-number=\"The field Units in stock must be a number.\" data-val-range=\"The field Units in stock must be between 0 and 2147483647.\" data-val-range-max=\"2147483647\" data-val-range-min=\"0\" data-val-required=\"The Units in stock field is required.\" id=\"UnitsInStock\" max=\"2147483647\" min=\"-2147483648\" name=\"UnitsInStock\" style=\"width:100%\" type=\"text\" value=\"0\" /\u003e\u003cscript\u003e\r\n\tkendo.syncReady(function(){jQuery(\"#UnitsInStock\").kendoNumericTextBox({\"format\":\"n0\",\"decimals\":0});});\r\n\u003c/script\u003e\u003cspan class=\"field-validation-valid\" data-valmsg-for=\"UnitsInStock\" data-valmsg-replace=\"true\"\u003e\u003c/span\u003e"
            }, {
                "title": "Discontinued",
                "headerAttributes": { "data-field": "Discontinued", "data-title": "Discontinued" },
                "width": "120px",
                "field": "Discontinued",
                "encoded": true,
                "editor": "\u003cinput class=\"k-checkbox\" data-val=\"true\" data-val-required=\"The Discontinued field is required.\" id=\"Discontinued\" name=\"Discontinued\" type=\"checkbox\" value=\"true\" /\u003e\u003clabel class=\"k-checkbox-label\" for=\"Discontinued\"\u003e\u003c/label\u003e\u003cinput name=\"Discontinued\" type=\"hidden\" value=\"false\" /\u003e\u003cspan class=\"field-validation-valid\" data-valmsg-for=\"Discontinued\" data-valmsg-replace=\"true\"\u003e\u003c/span\u003e"
            }, {
                "width": "250px",
                "command": [{ "name": "edit", "buttonType": "ImageAndText", "text": "Edit" }, { "name": "destroy", "buttonType": "ImageAndText", "text": "Delete" }]
            }
            ],
            "pageable": { "buttonCount": 10 },
            "sortable": true,
            "scrollable": { "height": "200px" },
            "editable": {
                "confirmation": "Are you sure you want to delete this record?",
                "confirmDelete": "Delete",
                "cancelDelete": "Cancel",
                "mode": "inline",
                "create": true,
                "update": true,
                "destroy": true
            },
            "toolbar": { "command": [{ "name": null, "buttonType": "ImageAndText", "text": "Add new record" }] },
            "messages": { "noRecords": "No records available." },
            "dataSource": {
                "type": (function() {
                    if (kendo.data.transports['aspnetmvc-ajax']) {
                        return 'aspnetmvc-ajax';
                    } else {
                        throw new Error('The kendo.aspnetmvc.min.js script is not included.');
                    }
                })(),
                "transport": {
                    "read": { "url": "/aspnet-mvc/Grid/EditingInline_Read" },
                    "prefix": "",
                    "update": { "url": "/aspnet-mvc/Grid/EditingInline_Update" },
                    "create": { "url": "/aspnet-mvc/Grid/EditingInline_Create" },
                    "destroy": { "url": "/aspnet-mvc/Grid/EditingInline_Destroy" }
                },
                "pageSize": 20,
                "page": 1,
                "total": 0,
                "serverPaging": true,
                "serverSorting": true,
                "serverFiltering": true,
                "serverGrouping": true,
                "serverAggregates": true,
                "filter": [],
                "error": error_handler,
                "schema": {
                    "data": "Data",
                    "total": "Total",
                    "errors": "Errors",
                    "model": { "id": "ProductID", "fields": { "ProductID": { "type": "number" }, "ProductName": { "type": "string" }, "UnitPrice": { "type": "number" }, "UnitsInStock": { "type": "number" }, "Discontinued": { "type": "boolean" }, "LastSupply": { "type": "date" }, "UnitsOnOrder": { "type": "number" }, "Category": { "type": "object" }, "CategoryID": { "type": "number", "defaultValue": null }, "QuantityPerUnit": { "type": "string" } } }
                }
            }
        });
});
//*/